package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("oss上传文件");
        //生成文件名 年-月-日-uuid.jpg
        String path = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
        path = path + UUID.randomUUID().toString();
        //获取文件后缀
        String filename = file.getOriginalFilename();
        path = path + filename.substring(filename.lastIndexOf("."));

        //上传文件至oss
        String url = aliOssUtil.upload(file.getBytes(), path);
        log.info("文件上传成功：文件名{}，访问路径{}",path,url);
        return Result.success(url);
    }
}
